package com.xyyl.patient.common.dypls;

import com.aliyuncs.dyplsapi.model.v20170525.BindAxbResponse;
import com.aliyuncs.dyplsapi.model.v20170525.UpdateSubscriptionResponse;
import com.aliyuncs.exceptions.ClientException;
import com.xyyl.patient.core.support.SpringContextHolder;
import com.xyyl.patient.core.util.DateUtils;
import com.xyyl.patient.entity.base.BindAxbEntity;
import com.xyyl.patient.service.base.BindAxbService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author kellie
 * @ClassName: AliyunDyplsService
 * @Description:阿里云号码隐私保护
 * @date 2017年5月24日
 */
@Component
public class AliyunDyplsService {

    private static Logger logger = LogManager.getLogger(AliyunDyplsService.class);
    private static BindAxbService bindAxbService;

    private static BindAxbService getbindAxbService() {
        if (bindAxbService == null) {
            bindAxbService = SpringContextHolder.getBean("bindAxbService");
        }
        return bindAxbService;
    }

    public static String BindAxb(String Aphone, String Bphone) {
        try {
            BindAxbResponse axbResponse = AliyunDyplsUtil.bindAxb(Aphone, Bphone);
            System.out.println("AXB绑定接口返回的结果----------------");
            System.out.println("Code=" + axbResponse.getCode());
            System.out.println("Message=" + axbResponse.getMessage());
            System.out.println("RequestId=" + axbResponse.getRequestId());
            if (axbResponse.getCode() != null && axbResponse.getCode().equals("OK")) {
                BindAxbEntity bindAxbEntity = new BindAxbEntity();
                //获取绑定关系对应的ID
                bindAxbEntity.setSubsId(Integer.parseInt(axbResponse.getSecretBindDTO().getSubsId()));
                bindAxbEntity.setAPhone(Aphone);
                bindAxbEntity.setBPhone(Bphone);
                //获取绑定关系对应的 中间手机号
                bindAxbEntity.setSecretPhone(axbResponse.getSecretBindDTO().getSecretNo());

                bindAxbEntity.setAddTime(DateUtils.currentDatetime());
                //设置过期时间
                bindAxbEntity.setExpTime(DateUtils.addHour(new Date(System.currentTimeMillis()), 6000));
                //获取绑定关系对应的 B端手机号
                //axbResponse.getSecretBindDTO().getExtension();

                //记录绑定
                try {
                    getbindAxbService().saveBindAxb(bindAxbEntity);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return axbResponse.getSecretBindDTO().getSecretNo();
            } else {
                return axbResponse.getMessage();
            }

        } catch (ClientException e) {
            e.printStackTrace();
        }

        return "";
    }

    //更新绑定关系  ,解绑原有Bphone  绑定新的 Bphone
    public static String updateSubscription(Integer subsId, String xPhone, String bPhone) {
        try {
            UpdateSubscriptionResponse response = AliyunDyplsUtil.updateSubscription(subsId, xPhone, bPhone);
            if(response ==null){
                return "";
            }
            if (response.getCode() != null && response.getCode().equals("OK")) {

                //请求成功   更新绑定关系
                BindAxbEntity bindAxbEntity = new BindAxbEntity();
                bindAxbEntity.setSubsId(subsId);
                bindAxbEntity.setBPhone(bPhone);
                bindAxbEntity.setAddTime(DateUtils.currentDatetime());
                getbindAxbService().updateBindAxb(bindAxbEntity);

            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "";
    }
}
